package com.kp.fintech.admin.intercepter;


import com.kp.fintech.admin.vo.LoginBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class UserContextInterceptor extends HandlerInterceptorAdapter {

    private static final Logger log = LoggerFactory.getLogger(UserContextInterceptor.class);

    @Resource
    private RedisTemplate redisTemplate;
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse respone, Object arg2) throws Exception {
        String tokenNo = request.getHeader("token");
        if (tokenNo != null) {
            logger.debug("拦截器中获取的到token是:{}",tokenNo);
            if (redisTemplate.opsForHash() != null) {
                Map<String, String> resultMap = redisTemplate.opsForHash().entries(tokenNo);
                String userName = resultMap.get("userName");
                request.setAttribute("userName", userName); //request设置userName, 在AOP中使用
            }
        }

        return true;
    }

/*
	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse respone, Object arg2, Exception arg3)
			throws Exception {

	}
	*/
}
